package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.NewSocietyOrganizationBean;
import com.sx.zongzhi.bean.UserPermission;
import com.sx.zongzhi.net.ServiceFactory;
import com.sx.zongzhi.presenter.NewSocietyOrganizationDetailPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.NewSocietyOrganizationDetailView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 新组织详情
 * Created by admin on 2017/12/9.
 */

public class NewSocietyOrganizationDetailActivity extends BaseActivity implements NewSocietyOrganizationDetailView {


    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.imv1)
    ImageView imv1;
    @BindView(R.id.imv_Head)
    ImageView imvHead;
    @BindView(R.id.rl_Head)
    RelativeLayout rlHead;
    @BindView(R.id.et_name)
    TextView etName;
    @BindView(R.id.sp_type)
    TextView spType;
    @BindView(R.id.et_registrationNumbers)
    TextView etRegistrationNumbers;
    @BindView(R.id.et_registrationManagementCode)
    TextView etRegistrationManagementCode;
    @BindView(R.id.et_unifiedSocialCreditCode)
    TextView etUnifiedSocialCreditCode;
    @BindView(R.id.et_address)
    TextView etAddress;
    @BindView(R.id.et_residence)
    TextView etResidence;
    @BindView(R.id.sp_state)
    TextView spState;
    @BindView(R.id.et_moneySource)
    TextView etMoneySource;
    @BindView(R.id.tv_approvalDate)
    TextView tvApprovalDate;
    @BindView(R.id.imv2)
    ImageView imv2;
    @BindView(R.id.sp_attentionExtent)
    TextView spAttentionExtent;
    @BindView(R.id.et_legalPersonName)
    TextView etLegalPersonName;
    @BindView(R.id.et_legalSafePerson)
    TextView etLegalSafePerson;
    @BindView(R.id.et_legalSafePersonMobileNumber)
    TextView etLegalSafePersonMobileNumber;
    @BindView(R.id.vi)
    View vi;
    @BindView(R.id.et_legalSafePersonTelephone)
    TextView etLegalSafePersonTelephone;
    @BindView(R.id.et_legalPerson)
    TextView etLegalPerson;
    @BindView(R.id.sp_cardType)
    TextView spCardType;
    @BindView(R.id.et_cardNo)
    TextView etCardNo;
    @BindView(R.id.et_legalPersonMobileNumber)
    TextView etLegalPersonMobileNumber;
    @BindView(R.id.vi1)
    View vi1;
    @BindView(R.id.et_legalPersonTelephone)
    TextView etLegalPersonTelephone;
    @BindView(R.id.rbt_Yes)
    RadioButton rbtYes;
    @BindView(R.id.rbt_No)
    RadioButton rbtNo;
    @BindView(R.id.rg_isHaveOverSea)
    RadioGroup rgIsHaveOverSea;
    @BindView(R.id.rbt_Yes1)
    RadioButton rbtYes1;
    @BindView(R.id.rbt_No1)
    RadioButton rbtNo1;
    @BindView(R.id.rg_isHaveOrganizationCondition)
    RadioGroup rgIsHaveOrganizationCondition;
    @BindView(R.id.rbt_Yes2)
    RadioButton rbtYes2;
    @BindView(R.id.rbt_No2)
    RadioButton rbtNo2;
    @BindView(R.id.rg_isHaveOrganization)
    RadioGroup rgIsHaveOrganization;
    @BindView(R.id.rbt_Yes3)
    RadioButton rbtYes3;
    @BindView(R.id.rbt_No3)
    RadioButton rbtNo3;
    @BindView(R.id.rg_isHaveGuild)
    RadioGroup rgIsHaveGuild;
    @BindView(R.id.rbt_Yes4)
    RadioButton rbtYes4;
    @BindView(R.id.rbt_No4)
    RadioButton rbtNo4;
    @BindView(R.id.rg_isHaveGroup)
    RadioGroup rgIsHaveGroup;
    @BindView(R.id.rbt_Yes5)
    RadioButton rbtYes5;
    @BindView(R.id.rbt_No5)
    RadioButton rbtNo5;
    @BindView(R.id.rg_isHaveWomenFederation)
    RadioGroup rgIsHaveWomenFederation;
    @BindView(R.id.et_partyMemberNumber)
    TextView etPartyMemberNumber;
    @BindView(R.id.ll_isHaveOrganization)
    RelativeLayout llIsHaveOrganization;
    @BindView(R.id.et_theMemeberOfGuild)
    TextView etTheMemeberOfGuild;
    @BindView(R.id.ll_isHaveGuild)
    RelativeLayout llIsHaveGuild;
    @BindView(R.id.et_theMemeberOfWomenFederation)
    TextView etTheMemeberOfWomenFederation;
    @BindView(R.id.ll_isHaveWomenFederation)
    RelativeLayout llIsHaveWomenFederation;
    @BindView(R.id.et_theMemeberOfGroup)
    TextView etTheMemeberOfGroup;
    @BindView(R.id.ll_isHaveGroup)
    RelativeLayout llIsHaveGroup;

    private String id;

    private NewSocietyOrganizationBean bean;
    private Disposable disposable;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_newsocietydetail);
        setTitleName("新社会组织详情");
        ButterKnife.bind(this);

        UserPermission userPermission = (UserPermission) getIntent().getSerializableExtra("userPermission");
        if (userPermission != null && userPermission.isDeleteNewSocietyOrganizations()){
            setRightImage(R.mipmap.icon_nav_delete);
        }
        if (userPermission != null && userPermission.isUpdateNewSocietyOrganizations()){
            setRightImage2(R.mipmap.icon_nav_edit);
        }
        id = getIntent().getStringExtra("id");
        LoadDialog.show(NewSocietyOrganizationDetailActivity.this, "加载中...");
        new NewSocietyOrganizationDetailPest(this).getNewSocietyOrganizationDetail(this, id);
        initRx();
    }

    private void initRx() {
        disposable = MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent bean) throws Exception {
                        if (bean.getRequest().equals("NewSocietyDetailActivity")) {
                            LoadDialog.show(NewSocietyOrganizationDetailActivity.this, "加载中...");
                            new NewSocietyOrganizationDetailPest(NewSocietyOrganizationDetailActivity.this)
                                    .getNewSocietyOrganizationDetail(NewSocietyOrganizationDetailActivity.this, id);
                        }
                    }
                });
    }

    @OnClick({R.id.imv_Right, R.id.imv_Right2,R.id.imv_Head})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.imv_Right:
                createDelete();
                break;
            case R.id.imv_Right2:
                if (bean == null) {
                    return;
                }
                startActivity(new Intent(this, UpdateNewSocietyOrganizationActivity.class).putExtra("bean", bean));
                break;
            case R.id.imv_Head:
                if (bean!=null&&!BaseUtil.isEmpty(bean.getImgUrl())){
                    String path=ServiceFactory.NEW_API_BASE_URL+File.separator+bean.getImgUrl();
                    startActivity(new Intent(this,ImagePlayActivity.class).
                            putExtra("path",path));
                }
                break;
        }
    }

    private void createDelete() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        CustomDialog dialog = builder.setMessage("确定删除此非公有制经济组织吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                new NewSocietyOrganizationDetailPest(NewSocietyOrganizationDetailActivity.this)
                        .deleteNewSocietyOrganization(NewSocietyOrganizationDetailActivity.this, bean.getId());
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(NewSocietyOrganizationDetailActivity.this);
        ToastUtils.showToast(NewSocietyOrganizationDetailActivity.this, message);
    }

    @Override
    public void getNewSocietyOrganizationDetailSuccess(NewSocietyOrganizationBean ecBean) {
        LoadDialog.dismiss(NewSocietyOrganizationDetailActivity.this);
        bean = ecBean;
        initData();
    }

    private void initData() {
        if (bean != null) {

            RequestOptions options = new RequestOptions().centerCrop()
                    .placeholder(R.drawable.shape_ff9f9f_12bgd)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .error(R.drawable.shape_ff9f9f_12bgd);
            Glide.with(this)
                    .load(ServiceFactory.NEW_API_BASE_URL + File.separator + bean.getImgUrl())
                    .apply(options)
                    .into(imvHead);
            etName.setText(BaseUtil.isEmpty(bean.getName()) ? "--" : bean.getName());
            spType.setText(bean.getType() == null ? "--" : bean.getType().getDisplayName());
            spCardType.setText(bean.getCardType() == null ? "--" : bean.getCardType().getDisplayName());
            spAttentionExtent.setText(bean.getAttentionExtent() == null ? "--" : bean.getAttentionExtent().getDisplayName());
            spState.setText(bean.getState() == 0 ? "存在" : "不存在");
            etResidence.setText(BaseUtil.isEmpty(bean.getResidence()) ? "--" : bean.getResidence());
            etRegistrationNumbers.setText(BaseUtil.isEmpty(bean.getRegistrationNumbers()) ? "--" : bean.getRegistrationNumbers());
            etRegistrationManagementCode.setText(BaseUtil.isEmpty(bean.getRegistrationManagementCode()) ? "--" : bean.getRegistrationManagementCode());
            etCardNo.setText(BaseUtil.isEmpty(bean.getCardNo()) ? "--" : bean.getCardNo());
            etUnifiedSocialCreditCode.setText(BaseUtil.isEmpty(bean.getUnifiedSocialCreditCode()) ? "--" : bean.getUnifiedSocialCreditCode());
            etAddress.setText(BaseUtil.isEmpty(bean.getAddress()) ? "--" : bean.getAddress());
            etMoneySource.setText(BaseUtil.isEmpty(bean.getMoneySource()) ? "--" : bean.getMoneySource());
            tvApprovalDate.setText(BaseUtil.isEmpty(bean.getApprovalDate()) ? "--" : bean.getApprovalDate());
            etLegalPersonName.setText(BaseUtil.isEmpty(bean.getLegalPersonName()) ? "--" : bean.getLegalPersonName());
            etLegalSafePerson.setText(BaseUtil.isEmpty(bean.getLegalSafePerson()) ? "--" : bean.getLegalSafePerson());
            etLegalSafePersonMobileNumber.setText(BaseUtil.isEmpty(bean.getLegalSafePersonMobileNumber()) ? "--" : bean.getLegalSafePersonMobileNumber());
            etLegalSafePersonTelephone.setText(BaseUtil.isEmpty(bean.getLegalSafePersonTelephone()) ? "--" : bean.getLegalSafePersonTelephone());
            etLegalPerson.setText(BaseUtil.isEmpty(bean.getLegalPerson()) ? "--" : bean.getLegalPerson());
            etLegalPersonMobileNumber.setText(BaseUtil.isEmpty(bean.getLegalPersonMobileNumber()) ? "--" : bean.getLegalPersonMobileNumber());
            etLegalPersonTelephone.setText(BaseUtil.isEmpty(bean.getLegalPersonTelephone()) ? "--" : bean.getLegalPersonTelephone());

//            rgIsHaveOverSea.setText(bean.isHaveOverSea() ? "是" : "否");
//            rgIsHaveOrganizationCondition.setText(bean.isHaveOrganizationCondition() ? "是" : "否");
//            rgIsHaveOrganization.setText(bean.isHaveOrganization() ? "是" : "否");
//            rgIsHaveGuild.setText(bean.isHaveGuild() ? "是" : "否");
//            rgIsHaveGroup.setText(bean.isHaveGroup() ? "是" : "否");
//            rgIsHaveWomenFederation.setText(bean.isHaveWomenFederation() ? "是" : "否");

            if (bean.isHaveOverSea()) {
                rbtYes.setChecked(true);
            } else {
                rbtNo.setChecked(true);
            }
            if (bean.isHaveOrganizationCondition()) {
                rbtYes1.setChecked(true);
            } else {
                rbtNo1.setChecked(true);
            }
            if (bean.isHaveOrganization()) {
                rbtYes2.setChecked(true);
            } else {
                rbtNo2.setChecked(true);
            }
            if (bean.isHaveGuild()) {
                rbtYes3.setChecked(true);
            } else {
                rbtNo3.setChecked(true);
            }
            if (bean.isHaveGroup()) {
                rbtYes4.setChecked(true);
            } else {
                rbtNo4.setChecked(true);
            }
            if (bean.isHaveWomenFederation()) {
                rbtYes5.setChecked(true);
            } else {
                rbtNo5.setChecked(true);
            }

            etPartyMemberNumber.setText(bean.getPartyNum() > 0 ? bean.getPartyNum() + "" : "--");
            etTheMemeberOfGuild.setText(bean.getTheMemeberOfGuild() > 0 ? bean.getTheMemeberOfGuild() + "" : "--");
            etTheMemeberOfWomenFederation.setText(bean.getTheMemeberOfWomenFederation() > 0 ? bean.getTheMemeberOfWomenFederation() + "" : "--");
            etTheMemeberOfGroup.setText(bean.getTheMemeberOfGroup() > 0 ? bean.getTheMemeberOfGroup() + "" : "--");

        }
    }

    @Override
    public void deleteNewSocietyOrganizationSuccess(Boolean bean) {
        LoadDialog.dismiss(NewSocietyOrganizationDetailActivity.this);
        MyRxBus.get().post(new MsgEvent("NewSocietyOrganizationActivity", 1, null));
        ToastUtils.showToast(NewSocietyOrganizationDetailActivity.this, "删除成功");
        finish();
    }

    @Override
    protected void onDestroy() {
        if (disposable != null) {
            disposable.dispose();
        }
        super.onDestroy();
    }

}
